package com.uab.ia.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uab.ia.R;
import com.uab.ia.entidades.Conexion;
import com.uab.ia.entidades.Estacion;

public class CaminoAdapter extends BaseAdapter {

	private Context ctx;
	private ArrayList<Conexion> datos;

	public CaminoAdapter(Context ctx, ArrayList<Conexion> datos) {
		super();
		this.ctx = ctx;
		this.datos = datos;
	}

	public int getCount() {
		return datos.size();
	}

	public Object getItem(int position) {
		return datos.get(position);
	}

	public long getItemId(int position) {
		return datos.get(position).getId_e1();
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		if ((convertView == null)
				|| !(convertView.getTag() instanceof ViewHolder)) {
			holder = new ViewHolder();

			LayoutInflater inflater = LayoutInflater.from(ctx);
			convertView = (LinearLayout) inflater.inflate(
					R.layout.row_conexion, null);

			holder.text1 = (TextView) convertView.findViewById(R.id.text_e1);
			holder.text2 = (TextView) convertView.findViewById(R.id.text_e2);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Estacion e = new Estacion(datos.get(position).getId_e1());
		holder.text1.setText(e.getNombre());
		if (datos.get(position).getId_e2() != 0) {
			e = new Estacion(datos.get(position).getId_e2());
			holder.text2.setText(e.getNombre());
		} else {
			holder.text2.setText("");
		}

		return convertView;
	}

	private class ViewHolder {
		TextView text1;
		TextView text2;
	}
}
